import Vue from "vue";
import VueRouter from "vue-router";

import Login from "@/views/login.vue";
import Index from "@/views/index.vue";
import Welcome from "@/views/welcome.vue";
import userList from "@/views/post/userlist.vue";
import postList from "@/views/post/postlist.vue";
import postPublish from "@/views/post/postpublish.vue";
import Category from "@/views/post/category.vue";
import postContent from "@/views/post/postcontent.vue";

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/index'
        },
        {
            name: 'login',
            path: '/login'
            , component: Login
        },
        {
            name: 'index',
            path: '/index'
            , component: Index,
            redirect: { name: 'welcome' },
            children: [
                {
                    name: 'welcome'
                    , path: 'welcome'
                    , component: Welcome
                },
                {
                    name: 'userlist'
                    , path: 'userlist'
                    , component: userList
                },
                {
                    name: 'postlist'
                    , path: 'postlist'
                    , component: postList
                },
                {
                    name: 'postcontent'
                    , path: 'postcontent'
                    , component: postContent
                },
                {
                    name: 'postpublish'
                    , path: 'postpublish/:id?'
                    , component: postPublish
                },
                {
                    name: 'category'
                    , path: 'category'
                    , component: Category
                },
            ]
        }]
})

// 导航守卫
router.beforeEach((to, form, next) => {
    console.log(to.name)
    if (to.name != 'login') {
        localStorage.getItem('heima_houtai') ? next() : next({ name: 'login' });
    } else {
        next()
    }
})
// 暴露
export default router 